﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;

namespace Oauth4Desktop
{
    public partial class Sample : Form
    {
        public Sample()
        {
            InitializeComponent();
            string[] strArr = new string[6] { "public_timeline", "friends_timeline", "user_timeline", "mentions", "comments_timeline", "comments_by_me"};
            this.methodsComboBox.DataSource = strArr;
        }

        oAuthSina oauth = new oAuthSina();

        private void GetRequestToken_Click(object sender, EventArgs e)
        {
            oauth.RequestTokenGet();
            this.rTokenBox.Text = oauth.token;
            this.rTokenSecretBox.Text = oauth.tokenSecret;
        }

        private void GetAuthlink_Click(object sender, EventArgs e)
        {
           
            this.SinaAuthLink.Text = oauth.AuthorizationGet();
        }

        private void GetAccessToken_Click(object sender, EventArgs e)
        {
            oauth.Verifier = this.pinBox.Text;
            oauth.AccessTokenGet();
            this.aTokenBox.Text = oauth.token;
            this.aTokenSecretBox.Text = oauth.tokenSecret;
        }

        private void invoke_Click(object sender, EventArgs e)
        {
            string basic = "http://api.t.sina.com.cn/";
            string url = "";
            switch (this.methodsComboBox.SelectedIndex)
            {
                case 0:
                    url = basic + "statuses/public_timeline.xml";
                    break;
                case 1:
                    url = basic + "statuses/friends_timeline.xml";
                    break;
                case 2:
                    url = basic + "statuses/user_timeline.xml";
                    break;
                case 3:
                    url = basic + "statuses/mentions.xml";
                    break;
                case 4:
                    url = basic + "statuses/comments_timeline.xml";
                    break;
                case 5:
                    url = basic + "statuses/comments_by_me.xml";
                    break;
                default:
                    break;
            }
            this.resultBox.Text = oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
        }

        private void Update_Click(object sender, EventArgs e)
        {
            string url = "http://api.t.sina.com.cn/statuses/update.xml?";
            this.resultTextBox2.Text = oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + HttpUtility.UrlEncode(statusText.Text));
        }

        private void Upload_Click(object sender, EventArgs e)
        {
            string url = "http://api.t.sina.com.cn/statuses/upload.xml?";
            this.resultTextBox2.Text = oauth.oAuthWebRequestWithPic(oAuthSina.Method.POST, url, "status=" + HttpUtility.UrlEncode(statusText.Text), filepath.Text);
        }

    }
}
